package com.youyiwen.Controller;

import com.youyiwen.Bean.Comment;
import com.youyiwen.Bean.Notice;
import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.NoticeMapper;
import com.youyiwen.Mapper.QuestionMapper;
import com.youyiwen.Service.CommentService;
import com.youyiwen.dto.CommentDTO;
import com.youyiwen.dto.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/26
 */
@Controller
public class CommentContoller {


    @Autowired
    private CommentService commentService;

    @Autowired
    private  NoticeMapper noticeMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @ResponseBody
    @PostMapping(value = "/comment1")
    public Object comment(@RequestBody CommentDTO commentDTO,
                          HttpServletRequest request) throws Exception {

        User user = (User) request.getSession().getAttribute("loginUser");
        if (user == null) {
            return ResultDTO.error(202, "请先登录");
        }


        Comment comment = new Comment();
        comment.setParentId(commentDTO.getParentId());
        comment.setContent(commentDTO.getContent());
        comment.setType(commentDTO.getType());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(System.currentTimeMillis());
        comment.setCommentator("153");
        comment.setLikeCount(0);
        commentService.insertComment(comment);
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("message", "成功");
        return hashMap;
    }


    @Value("${question.url}")
    String questionUrl;

    @PostMapping(value = "/comment/{id}")
    public Object comm(@PathVariable(name = "id") String id,
                       @RequestParam("comment") String text,
                       HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("loginUser");
        if (user == null) {
            return "login";
        }

        String userName = ((User) request.getSession().getAttribute("loginUser")).getUserName();

        Long current = System.currentTimeMillis();
        //添加评论
        Comment comment = new Comment();
        String commentId = userName + current.toString();
        comment.setId(commentId);
        comment.setParentId(id);
        comment.setContent(text);
        comment.setType(1);
        comment.setGmtCreate(current);
        comment.setGmtModified(current);
        comment.setCommentator(userName);
        comment.setLikeCount(0);
        questionMapper.updateCommentCount(id);
        commentService.insertComment(comment);

        //添加通知
        //别人回复自己的时候才添加
        String questionUserName = questionMapper.selectUserNameById(id);

        if (userName != questionUserName)
        {
            String title = questionMapper.selectTitleById(id);
            String noticeContent = userName + " 回复了您的问题：";
            Notice notice = new Notice();
            notice.setId(userName + "Notice" + current);
            notice.setContent(noticeContent);
            notice.setTitle(title);
            notice.setQuestionUrl(questionUrl + id);
            notice.setUserName(questionUserName);
            notice.setType(1);
            notice.setHasRead(0);
            notice.setGmtCreate(current);
            notice.setParentId(commentId);
            noticeMapper.insertNotice(notice);
        }
        return "redirect:/question/" + id;
    }

    @GetMapping("/delete/comment/{id}/{parentId}")
    public String delectCommentById(@PathVariable(value = "id")String id){
        commentService.delectCommentById(id);
        return "redirect:/question/{parentId}";
    }
}
